﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;



namespace T09TypeChange
{
    class Program
    {
        static void Main(string[] args)
        {
            #region 值类型转换
            //隐式类型转换
            double d = 10;
            //显式类型转换 又称为强制类型
            int a = (int)d;
            Console.WriteLine($"强制类型{a}");

            Console.WriteLine("=====================");

            //字符串与数值之间的转换
            string strNumber = "123";
            int iC = int.Parse(strNumber);
            //int iC=Int32.Parse(strNumber)
            Console.WriteLine($"字符串转换:{iC}");

            int _iD;
            //int iD = int.TryParse("123s");
            bool bD = int.TryParse("123s", out _iD);

            //转换类
            int iE = Convert.ToInt32("12");
            Console.WriteLine($"Convert类转换字符串{iE}");

            //数值转换字符串
            Console.WriteLine($"数值转字符串{12.ToString()}");

            Console.WriteLine("=====================");

            #endregion

            #region 引用类型转换
            //隐式类型转换(派生类到基类)
            Animal dog = new Dog();
            Animal cock = new Cock();

            dog.Walk();
            dog.Eat();

            cock.Walk();
            cock.Eat();
            //强制类型转换（基类到派生类）
            ((Cock)cock).Crow();
            (cock as Cock).Crow();

            #endregion

            Console.ReadKey();
        }
    }
    /// <summary>
    /// 抽象类 - Animal
    /// </summary>
    abstract class Animal
    {
        public abstract void Walk();

        public void Eat()
        {
            Console.WriteLine("吃");
        }
    }

    class Dog : Animal
    {
        public override void Walk()
        {
            Console.WriteLine("四条腿行走");
        }
    }

    class Cock : Animal
    {
        public override void Walk()
        {
            Console.WriteLine("两条腿行走");
        }
        public void Crow()
        {
            Console.WriteLine("公鸡报晓");
        }
    }
}
